import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:memo/state/model/todo.dart';
import 'package:memo/state/work_record_state.dart';
import 'package:memo/widget/todo_item_widget.dart';

import 'package:provider/provider.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';


class TodoListPage extends StatefulWidget {
  @override
  _TodoListPageState createState() => _TodoListPageState();
}

class _TodoListPageState extends State<TodoListPage> {
  GlobalKey<EasyRefreshState> _easyRefreshKey = GlobalKey<EasyRefreshState>();
  GlobalKey<RefreshHeaderState> _headerKey = GlobalKey<RefreshHeaderState>();
  GlobalKey<RefreshFooterState> _footerKey = GlobalKey<RefreshFooterState>();
  bool _loadMore = true;

  @override
  Widget build(BuildContext context) {
    return Consumer<WorkRecordState>(builder: (context, state, _) {
      return EasyRefresh(
        key: _easyRefreshKey,
        behavior: ScrollOverBehavior(),
        refreshHeader: _refreshHeader(),
        refreshFooter: _refreshFooter(),
        child: ListView.builder(
          padding: EdgeInsets.only(top: 2.0),
          itemBuilder: (context, index) {
            final todo = state.todoList[index];
            return TodoItemWidget(
              todo: todo,
              key: Key(todo.id),
              onLongPress: () async {
                bool flag = await deleteDialog(context);
                if (flag) {
                  await state.delete(todo);
                }
              },
            );
          },
          itemCount: state.len(),
        ),
        onRefresh: () async {
          await Future.delayed(const Duration(seconds: 1), () {
            _easyRefreshKey.currentState.waitState(() {
              setState(() {
                _loadMore = true;
              });
            });
          });
        },
        loadMore: _loadMore
            ? () async {
                await new Future.delayed(const Duration(seconds: 1), () async {
                  if (state.len() < 30) {
                    await state.loadMoreTodo();
                  } else {
                    _easyRefreshKey.currentState.waitState(() {
                      setState(() {
                        _loadMore = false;
                      });
                    });
                  }
                });
              }
            : null,
      );
    });
  }

  Widget _refreshHeader() {
    return ClassicsHeader(
      key: _headerKey,
      refreshText: "下拉刷新",
      refreshReadyText: "释放刷新",
      refreshingText: "正在刷新...",
      refreshedText: "刷新结束",
      moreInfo: "更新于 ${TimeOfDay.now().format(context)}",
      bgColor: Colors.transparent,
      textColor: Colors.black87,
      moreInfoColor: Colors.black54,
      showMore: true,
    );
  }

  Widget _refreshFooter() {
    return ClassicsFooter(
      key: _footerKey,
      loadText: "上拉加载",
      loadReadyText: "释放加载",
      loadingText: "正在加载",
      loadedText: "加载结束",
      noMoreText: "没有更多数据",
      moreInfo: "更新于 ${TimeOfDay.now().format(context)}",
      bgColor: Colors.transparent,
      textColor: Colors.black87,
      moreInfoColor: Colors.black54,
      showMore: true,
    );
  }
}
